<?php
namespace app_supplier\controllers;

use app_supplier\models\User;
use yii\web\Controller;
use yii\web\Response;
use yii;
use yii\helpers\Url;

class BaseController extends Controller
{
    public $layout = 'main';
    public $pageTitle = '列表';
    //该经销商id
    protected $dealer_id = 9;

    //是否打印
    protected $issetPrint = 1;
    //获取参数
    public $params = [];

    public function beforeAction($action)
    {
        //判断用户是否登录
        $this->view->registerMetaTag(['name' => 'keywords', 'content' => '车保无忧 汽车保养 门店系统'], 'keywords');
        $this->view->registerMetaTag(['name' => 'description', 'content' => '瓦肯科技以云服务的形式为汽车养护服务商家提供系统管理、客户维护、网络营销等的整体解决方案。'], 'description');

        $sort = Yii::$app->request->get('sort', 'create_time');
        $order = Yii::$app->request->get('order', 'desc');
        
        $this->params = [
            'sort'=>[
                $sort=>$order
            ],
            'page'=>[
                'page'=>Yii::$app->request->get('page', 1),
                'pageSize'=>!empty(Yii::$app->params['pageSize']) ? Yii::$app->params['pageSize'] : 50,
                'url'=>Url::to([$this->id . '/'.$this->action->id]),
            ],
            'order'=>$order,
            'sortField'=>$sort
        ];

        if (!Yii::$app->session->has('user_dealer')) {
            if (Yii::$app->user->isGuest) {
                return  $this->returnBack(['user/login']);
            }

            $list  = User::findUserInfo(Yii::$app->user->getIdentity()->account, Yii::$app->user->getIdentity()->password);
            Yii::$app->session->set('user_dealer', $list);
        }
        return parent::beforeAction($action);
    }
    public function returnAjax($data = [])
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        return $data;
    }
    public function returnBack($url)
    {
        return  Yii::$app->response->redirect($url)->send();
    }
}
